package com.ruoyi.web.controller.buy;

import com.ruoyi.buy.domain.entity.PO.*;
import com.ruoyi.buy.domain.entity.buy0003.Buy0003GetInfoByIdVO;
import com.ruoyi.buy.domain.entity.importPO.BuyBatchPurchaseBillImport;
import com.ruoyi.buy.service.IBuyPurchaseBillService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/buy/buy0003")
public class buy0003Controller extends BaseController
{
    @Autowired
    private IBuyPurchaseBillService buyPurchaseBillService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BuyPurchaseBill buyPurchaseBill)
    {
        startPage();
        List<BuyPurchaseBill> list = buyPurchaseBillService.selectBuyPurchaseBillList(buyPurchaseBill);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyPurchaseBill buyPurchaseBill)
    {
        List<BuyPurchaseBill> list = buyPurchaseBillService.selectBuyPurchaseBillList(buyPurchaseBill);
        ExcelUtil<BuyPurchaseBill> util = new ExcelUtil<>(BuyPurchaseBill.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(buyPurchaseBillService.selectBuyPurchaseBillById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "保存合同信息", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Buy0003GetInfoByIdVO para) throws Exception {
        para.setCreateBy(getUsername());
        return toAjax(buyPurchaseBillService.saveBuyPurchaseBill(para));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyPurchaseBill buyPurchaseBill)
    {
        return toAjax(buyPurchaseBillService.updateBuyPurchaseBill(buyPurchaseBill));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "删除采购合同", businessType = BusinessType.DELETE)
	@PostMapping("/delBill")
    public AjaxResult remove(@RequestBody BuyPurchaseBill para)
    {
        para.setUpdateBy(getUsername());
        return toAjax(buyPurchaseBillService.updateBuyPurchaseBill(para));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BuyPurchaseBillItem> util = new ExcelUtil<>(BuyPurchaseBillItem.class);
        util.importTemplateExcel(response, "采购合同明细数据");
    }
    /**
     * 获取【请填写功能名称】详细信息
     */
    @PostMapping(value = "/getInvoiceInfo")
    public AjaxResult getInvoiceInfo(@RequestBody BuyPurchaseInvoice para)
    {
        return success(buyPurchaseBillService.selectBuyPurchaseInvoiceList(para));
    }

    @Log(title = "删除采购合同对应发票", businessType = BusinessType.DELETE)
    @PostMapping("/delInvoice")
    public AjaxResult remove(@RequestBody BuyPurchaseInvoice para)
    {
        para.setUpdateBy(getUsername());
        para.setStatus("1");
        para.setDelFlag("1");
        return toAjax(buyPurchaseBillService.updateBuyPurchaseInvoice(para));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "保存发票", businessType = BusinessType.INSERT)
    @PostMapping("/saveInvoice")
    public AjaxResult saveInvoice(@RequestBody Buy0003GetInfoByIdVO para)
    {
        para.setCreateBy(getUsername());
        return toAjax(buyPurchaseBillService.saveInvoice(para));
    }

    @PostMapping("/importData")
    public AjaxResult importBatch(MultipartFile file) throws Exception
    {
        ExcelUtil<BuyBatchPurchaseBillImport> util = new ExcelUtil<>(BuyBatchPurchaseBillImport.class);
        List<BuyBatchPurchaseBillImport> list = util.importExcel(file.getInputStream());
        buyPurchaseBillService.importBatchSave(list);
        return success(list);
    }

    @PostMapping(value = "/getNotPostItem")
    public AjaxResult getNotPostItem(@RequestBody String vContractNos)
    {
        return success(buyPurchaseBillService.getNotPostItem(vContractNos));
    }

    @PostMapping(value = "/getPostItem")
    public AjaxResult getPostItem(@RequestBody BuyPurchasePost para)
    {
        return success(buyPurchaseBillService.getPostItem(para));
    }

    @PostMapping(value = "/savePostItem")
    public AjaxResult savePostItem(@RequestBody List<BuyPurchasePostItem> para)
    {
        return success(buyPurchaseBillService.savePostItem(para));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "修改合同状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBillStatus")
    public AjaxResult updateBillStatus(@RequestBody BuyPurchaseBill para) throws Exception {
        para.setUpdateBy(getUsername());
        return toAjax(buyPurchaseBillService.updateBillStatus(para));
    }
}

